﻿//
// Created by Administrator on 2022/6/15.
//

#include "NotificationIcon.h"
#include "MainFrame.h"

#include <wx/menu.h>

const int NotificationIcon::PopupExitID = wxID_HIGHEST + 1;

NotificationIcon::NotificationIcon() : wxTaskBarIcon(){
    mainFrame = nullptr;
}

NotificationIcon::~NotificationIcon()= default;

void NotificationIcon::SetMainFrame(MainFrame *frame){
    mainFrame = frame;
}

void NotificationIcon::OnLeftDoubleClick(wxTaskBarIconEvent &event){
    if(mainFrame)
        mainFrame->Show(!mainFrame->IsShown());
}

wxMenu* NotificationIcon::CreatePopupMenu(){
    auto *popup = new wxMenu;

    popup->AppendSeparator();
    popup->Append(PopupExitID, wxT("E&xit"));
    return popup;
}

void NotificationIcon::OnQuit(wxCommandEvent& WXUNUSED(event)){
    /*
    if(mainFrame) {
        mainFrame->Close(false);
    }*/
    RemoveIcon();
    if ( wxMessageBox(wxT("确定要退出吗"), wxT("请确认"),
                      wxICON_QUESTION | wxYES_NO) == wxYES ){
        ExitProcess( 0 );
    }
}


BEGIN_EVENT_TABLE(NotificationIcon, wxTaskBarIcon)
                EVT_TASKBAR_LEFT_DCLICK(NotificationIcon::OnLeftDoubleClick)
                EVT_MENU(PopupExitID, NotificationIcon::OnQuit)
END_EVENT_TABLE()